Page({
  data: {
    // 选项列表
    items: [
      { id: '1', name: '选项一', checked: false },
      { id: '2', name: '选项二', checked: false },
      { id: '3', name: '选项三', checked: false },
      { id: '4', name: '选项四', checked: false },
      { id: '5', name: '选项五', checked: false }
    ],
    isAllSelected: false, // 是否全选
    selectedCount: 0, // 选中数量
    showResult: false, // 是否显示结果
    selectedItems: [] // 选中的项
  },

  // 单个选项变化时触发
  onCheckboxChange(e) {
    const selectedIds = e.detail.value;
    const items = this.data.items.map(item => {
      item.checked = selectedIds.includes(item.id);
      return item;
    });

    // 判断是否全选
    const isAllSelected = items.every(item => item.checked);
    
    this.setData({
      items,
      isAllSelected,
      selectedCount: selectedIds.length
    });
  },

  // 全选/取消全选
  onSelectAllChange(e) {
    const isSelected = e.detail.value.includes('all');
    const items = this.data.items.map(item => {
      item.checked = isSelected;
      return item;
    });

    this.setData({
      items,
      isAllSelected: isSelected,
      selectedCount: isSelected ? items.length : 0
    });
  },

  // 提交选中项
  submitSelected() {
    // 获取选中的项
    const selectedItems = this.data.items.filter(item => item.checked);
    
    if (selectedItems.length === 0) {
      wx.showToast({
        title: '请至少选择一项',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    // 显示结果
    this.setData({
      showResult: true,
      selectedItems
    });

    // 这里可以添加实际提交逻辑，比如调用API
    console.log('提交的选中项：', selectedItems);
    
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 2000
    });
  }
});
